package Sort;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class BinarySearchTest2 {

	int[] testArray;
	@Before
	public void setUp() throws Exception {
	}

	@Test
	public void testBuggyBinarySearch() {
		
		int[] arrayWith42 = new int[] { 1, 4, 42, 55, 67, 87, 100, 
				 245 }; 
		
		 assertEquals(2, BinarySearch.buggyBinarySearch(arrayWith42, 42));
		 assertEquals(-1, BinarySearch.buggyBinarySearch(arrayWith42, -12));
	}
	
	 @Test 
	  public void searchEmptyArray() { 
	      testArray = new int[] {}; 
	      assertEquals(-1, BinarySearch.buggyBinarySearch(testArray, 42)); 
	 } 

	 @Test
	 public void searchArrayOfSizeOne() { 
	      testArray = new int[] { 42 }; 
	      assertEquals(0, BinarySearch.buggyBinarySearch(testArray, 42)); 
	      assertEquals(-1, BinarySearch.buggyBinarySearch(testArray, 43)); 
	 } 
	 
	 @Test
	 public void calculateMidpointWithBoundaryValues() { 
	      
		  assertEquals(0, BinarySearch.calculateMidpoint (0, 1)); 
	      assertEquals(1, BinarySearch.calculateMidpoint (0, 2)); 

	      assertEquals(1200000000, BinarySearch.calculateMidpoint(1100000000, 1300000000)); 
	      assertEquals(Integer.MAX_VALUE - 2,   BinarySearch.calculateMidpoint  (Integer.MAX_VALUE-2, 
	Integer.MAX_VALUE-1)); 
	      assertEquals(Integer.MAX_VALUE - 1,  BinarySearch.calculateMidpoint  (Integer.MAX_VALUE-1, 
	Integer.MAX_VALUE)); 
	 }

	 @Test 
	  public void testBoundaryCasesForItemLocation() { 
	      testArray = new int[] { -324, -3, -1, 0, 42, 99, 101 }; 
	      assertEquals(0,  BinarySearch.buggyBinarySearch(testArray, -324));  // 	first position 
	      assertEquals(3, BinarySearch.buggyBinarySearch(testArray, 0));      // 	middle position 
	      assertEquals(6, BinarySearch.buggyBinarySearch(testArray, 101));   // 	last position 
	 } 
}


